<?php
// +----------------------------------------------------------------------
// | 品达网络 [ 品质保证，助企腾达 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.pdwl.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 萧涩啊 <wangmode@163.com>
// +----------------------------------------------------------------------
// | 工资管理

namespace app\models;
use think\Exception;



class WageModel extends BaseModel
{
    protected $table = 'sh_wage';
    const FULL_ATTENDANCE = 200;

    /**
     * 工资列表
     * @param $page
     * @param $limit
     * @param int $cycle_id
     * @return array|\PDOStatement|string|\think\Collection|\think\model\Collection
     */
    public function getListById($page,$limit,$cycle_id = 0){
        try {
            $where['cycle_id'] = $cycle_id;
            return $this->alias('w')->where($where)->leftJoin('employee e','w.employee_id=e.id')->field('w.*,e.name')->limit(($page-1)*$limit,$limit)->select();
        }catch (Exception $exception){
            $this->setErrorInfo($exception->getMessage());
        }
    }

    public function add($data)
    {
        
    }

    /**
     * 设置全勤
     * @param $wage_id
     * @param $is_full
     * @return bool
     */
    public function setFull($wage_id,$is_full)
    {
        try{
            $info = $this->where('id',$wage_id)->find();
            if($info['status'] !=0){
                throw new Exception('当前状态不可进行该操作！');
            }
            $full_attendance            = $is_full==1?self::FULL_ATTENDANCE:0;
            $data['is_full']            = $is_full;
            $data['full_attendance']    = $full_attendance;
            $data['wage']               = $this->wageCalculation($info['wages_basic'],$info['bonus'],$full_attendance,$info['deduction']);
            $this->where('id',$wage_id)->update($data);
            return true;
        }catch (\Exception $exception){
            $this->setErrorInfo($exception->getMessage());
        }
    }

    /**
     * @param $wage_id
     * @param $bonus
     * @param $deduction
     * @return bool
     */
    public function setValue($wage_id,$bonus,$deduction,$remark)
    {
        try{
            $info = $this->where('id',$wage_id)->find();
            if($info['status'] !=0){
                throw new Exception('当前状态不可进行该操作！');
            }

            $data['remark']              = $remark;
            $data['bonus']              = $bonus;
            $data['deduction']          = $deduction;
            $data['wage']               = $this->wageCalculation($info['wages_basic'],$bonus,$info['full_attendance'],$deduction);
            $this->where('id',$wage_id)->update($data);
            return true;
        }catch (\Exception $exception){
            $this->setErrorInfo($exception->getMessage());
        }
    }

    /**
     * 计算工资
     * @param $basic        //基本工资
     * @param $bonus        //奖金
     * @param $full         //全勤
     * @param $deduction    //扣费
     * @return mixed
     */
    public function wageCalculation($basic,$bonus,$full,$deduction)
    {
        return $basic+$bonus+$full-$deduction;
    }


    public function wageOperate($wage_id,$amount)
    {
        $info       = $this->where('id',$wage_id)->find();
        $wage       = $info['wage'];
        $is_paid    = $info['wages_paid'];
        $paid       = $amount + $is_paid;
        $remain     = $wage - $paid;
        try{
            if($remain<0){
                throw new Exception('金额超过总工资');
            }elseif ($remain==0){           //工资发完
                $data['wages_paid']  = $paid;
                $data['status']     = 2;
                $this->where('id',$wage_id)->update($data);
                return true;
            }else{                          //发一部分
                $data['wages_paid']  = $paid;
                $data['status']     = 1;
                $this->where('id',$wage_id)->update($data);
                return true;
            }
        }catch (\Exception $exception){
            $this->setErrorInfo($exception->getMessage());
        }
    }


}
